* This do file calculates assesse the substantive size of the OLPR-effect
* More specifically, it shows length of the ballot, number of regional seats and 
* calculates the last winner-first loser difference in candidate vote share 


version 14 
* datasets are also Stata version 14


* set directory here (the folder where the subfolders are located)
* global repldirjop "insert here"
cd "$repldirjop"

capture log close
log using "tables\substantive_size.log", replace

use tables\candidates_csu_replication.dta, clear
keep if year == 2013
decode rbez, gen(wk)
replace wk = subinstr(wk,"OLPR ballot ","",.)
gen listrank = list_pre
merge 1:1 year wk listrank using tables\votes2013, keepusing(gew) // merge info regarding whether elected or not (data from election authorities)
assert _m == 3 

byso wk: egen listlength = max(listrank)
gen tmp = gew != ""
by wk: egen sum_elected_region = total(tmp)
drop tmp

* sort by regional district (wk) and ranking based on second vote (defined as for the dep. var. in the analyses)
gsort wk -sv_cand
by wk: gen svcandrank = _n

by wk: gen winlosediff = sv_cand-sv_cand[_n-1] if svcandrank == sum_elected_region + 1
table wk, c(mean listlength mean sum_elected_region mean winlose)
* br wk listrank svcandrank sv_cand sum_elected


log close

